package com.scau.yangsuiyu.estore.controller.seller.manageOrder;

import com.scau.yangsuiyu.estore.controller.myComponent.BuyerOrderHBox;
import com.scau.yangsuiyu.estore.controller.myComponent.SellerOrderHBox;
import com.scau.yangsuiyu.estore.dababases.dao.OrderDetailDao;
import com.scau.yangsuiyu.estore.dababases.entity.OrderDetail;
import com.scau.yangsuiyu.estore.dababases.entity.Orders;
import com.scau.yangsuiyu.estore.dababases.entity.Seller;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Pagination;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 卖家订单管理界面Controller
 * @date 2019-06-08 15:21
 * @email suiyu_yang@163.com
 */
@FXMLController
public class ManageSellerOrder extends AnchorPane implements Initializable {
    private Seller seller;
    private List<OrderDetail> orderDetails;
    private int itemsPerPage = 5;
    private VBox orderDetailsVBox = new VBox();
    @FXML
    private VBox mainVBox;

    @FXML
    private Pagination pagination;

    public ManageSellerOrder() {

    }

    public ManageSellerOrder(Seller seller) {
        this.seller = seller;
        this.orderDetails = new OrderDetailDao().getOrderDetailBySeller(seller);
        System.out.println(orderDetails.size());
        loadFXML();
    }


    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/seller/manageOrder/ManageOrder.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        updatePages();

        mainVBox.getChildren().add(orderDetailsVBox);
    }

    /**
     * 更新页面
     */
    private void updatePages() {
        orderDetailsVBox.getChildren().clear();

        int pageNum;
        if (orderDetails.size() % itemsPerPage == 0) {
            pageNum = orderDetails.size() / itemsPerPage;
        } else {
            pageNum = orderDetails.size() / itemsPerPage + 1;
        }
        pagination.setPageCount(pageNum);
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
    }

    /**
     * 创建每一页的对象
     *
     * @param pageIndex
     * @return
     */
    private VBox createPage(int pageIndex) {
        int page = pageIndex * itemsPerPage; //当前是第几个商品
        if (orderDetails.size() != 0) {
            orderDetailsVBox.getChildren().clear();
            for (int i = page; i < page + itemsPerPage; i++) {
                if (i < orderDetails.size()) {
                    OrderDetail temp = orderDetails.get(i);
                    SellerOrderHBox sellerOrderHBox = new SellerOrderHBox(temp);
                    orderDetailsVBox.getChildren().add(sellerOrderHBox);
                }
            }
        }
        return orderDetailsVBox;
    }

}
